package com.dream.app.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.app.cache.AppOrgCache;
import com.dream.app.service.OrgService;
import com.dream.base.controller.BaseController;
import com.dream.master.service.ShopService;
import com.dream.model.master.Shop;
import com.dream.model.master.Shop.ShopType;
import com.dream.utils.annotation.AccessLog;
import com.dream.utils.annotation.SkipAuthority;
import com.dream.utils.hibernate.PagingVO;
import com.dream.utils.log.DreamLogUtils;
import com.dream.vo.base.FirstGroup;
import com.dream.vo.base.MvcValidtorResult;
import com.dream.vo.base.ResultType;
import com.dream.vo.master.ShopVO;
import com.google.common.collect.Maps;

/**
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2013/9/29
 */
@Controller
@RequestMapping(value = "/shop")
public class ShopController extends BaseController{
	
	@Autowired
	private AppOrgCache orgCache;
	
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private OrgService orgService;
	
	@RequestMapping(value = "/list")
	@SkipAuthority
	@AccessLog(module="shop",content="网点列表")
	public String list(Model model,ShopVO vo,PagingVO page) {
		
		PagingVO pageVO = shopService.query(false,vo, page,this.getUser());
		model.addAttribute("pageVO", pageVO);
		return "master/shop/data/shop_list_data";
	}
	
	@RequestMapping(value = "/list/init")
	@SkipAuthority
	@AccessLog(module="shop",content="网点列表")
	public String listInit(Model model) {
		
		model.addAttribute("shopTypes", ShopType.getTypes());
		model.addAttribute("firstOrgs",orgService.getFirstOrgOfMine(this.getUser()));
		return "shop_list";
	}
	
	@RequestMapping(value = "/list/pop/init/{shopType}")
	@SkipAuthority
	@AccessLog(module="shop",content="网点列表")
	public String listPopInit(Model model,@PathVariable String shopType) {
		
		model.addAttribute("shopType", shopType);
		model.addAttribute("firstOrgs",orgService.getFirstOrgOfMine(this.getUser()));
		return "master/shop/shop_pop_list";
	}
	
	@RequestMapping(value = "/list/pop")
	@SkipAuthority
	@AccessLog(module="shop",content="网点列表")
	public String listPop(Model model,ShopVO vo,PagingVO page) {
		
		PagingVO pageVO = shopService.query(false,vo, page,this.getUser());
		model.addAttribute("pageVO", pageVO);
		return "master/shop/data/shop_pop_list_data";
	}
	
	@RequestMapping(value = "/list/pop/orginit/{shopType}/{org}")
	@SkipAuthority
	@AccessLog(module="shop",content="网点列表")
	public String listPopInit(Model model,@PathVariable String shopType,@PathVariable Long org) {
		
		model.addAttribute("shopType", shopType);
		model.addAttribute("org",org);
		return "master/shop/shop_pop_org_list";
	}
	
	@RequestMapping(value = "/list/orgpop/{org}")
	@SkipAuthority
	@AccessLog(module="shop",content="网点列表")
	public String listShopPop(Model model,@PathVariable Long org, ShopVO vo,PagingVO page) {
		
		vo.getOrgs().add(org);
		PagingVO pageVO = shopService.queryShop(vo, page,this.getUser());
		model.addAttribute("pageVO", pageVO);
		return "master/shop/data/shop_pop_list_data";
		
	}
	
	
	@RequestMapping(value = "/update/init")
	@SkipAuthority
	@AccessLog(module="shop",content="网点初始化")
	public String shopInit(Model modelMap,String shopId) {
		
		Shop shop = shopService.queryById(shopId);
		modelMap.addAttribute("isNew", shop == null);
		if(shop != null){
			modelMap.addAttribute("shop", shop);
			modelMap.addAttribute("shopType", shop.getShopType().name());
		}else{
			modelMap.addAttribute("shopType", "shop");
		}
		modelMap.addAttribute("firstOrgs",orgService.getFirstOrgOfMine(this.getUser()));
		modelMap.addAttribute("shopTypes", ShopType.getTypes());
		return "shop_update";
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/update")
	@AccessLog(module="shop",content="网点更新")
	@SkipAuthority
	public Map<String,Object> shopoeUpdate(Model modelMap,@Validated({FirstGroup.class}) ShopVO vo,BindingResult valResult) {
		
		Map<String,Object> result = Maps.newHashMap();
		MvcValidtorResult errors = populateErrors(valResult);
		
		if(valResult.hasErrors()){
			result.put(RESULT_TYPE, ResultType.INPUT.toInteger());
			result.put(RESULT_CONTENT, errors);
			
		}else{
			try{
				if(StringUtils.isBlank(vo.getId()) && StringUtils.isBlank(vo.getSecondOrg())){
					this.addFieldMessage(errors, "secondOrg", "err0001");
				}else{
					shopService.update(vo, getUser());
					result.put(RESULT_TYPE,ResultType.SUCCESS.toInteger());
				}
			}catch(Exception ex){
					result.put(RESULT_TYPE, ResultType.EXCEPTION.toInteger());
					DreamLogUtils.error(this,"shopoeUpdate","update shop  occured exception", ex);
			}
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete/{id}")
	@AccessLog(module="shop",content="删除网点")
	@SkipAuthority
	public  Map<String, Object> shopDelete(@PathVariable String id) {
		
		Map<String,Object> result = Maps.newHashMap();
		try {
			shopService.delete(id, getUser());
			result.put(RESULT_TYPE, ResultType.SUCCESS.toInteger());
		} catch (Exception ex) {
			result.put(RESULT_TYPE,ResultType.ERROR.toInteger());
			DreamLogUtils.error(this, "shopDelete","delete shop item occured exception", ex);
		}
		return result;
	}
	
	@RequestMapping(value = "/user/list/pop/{id}")
	@SkipAuthority
	@AccessLog(module="shop",content="网点用户")
	public String shopUserPop(Model modelMap,@PathVariable String id) {
		
		Shop shop = shopService.queryById(id);
		Long orgId = shop.getOrgId();
		modelMap.addAttribute("secondOrg",orgId);
		
		return "master/shop/shop_scope_user_list";
		
	}
	
	@RequestMapping(value = "/user/list")
	@SkipAuthority
	@AccessLog(module="shop",content="网点人员列表")
	public String userList(Model model,ShopVO vo,PagingVO page) {
		
		PagingVO pageVO = shopService.queryShopUser(vo, page,this.getUser());
		model.addAttribute("pageVO", pageVO);
		return "master/shop/data/shop_user_list_data";
	}
	
	@ResponseBody
	@RequestMapping(value = "/user/delete/{id}")
	@AccessLog(module="shop",content="删除网点人员")
	@SkipAuthority
	public  Map<String, Object> shopUserDelete(@PathVariable Long id) {
		
		Map<String,Object> result = Maps.newHashMap();
		try {
			shopService.deleteShopUser(id, getUser());
			result.put(RESULT_TYPE, ResultType.SUCCESS.toInteger());
		} catch (Exception ex) {
			result.put(RESULT_TYPE,ResultType.ERROR.toInteger());
			DreamLogUtils.error(this, "shopUserDelete","delete shop user occured exception", ex);
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/user/add/{id}")
	@AccessLog(module="shop",content="删除网点人员")
	@SkipAuthority
	public  Map<String, Object> shopUserAdd(@PathVariable String id,String[] userId) {
		
		Map<String,Object> result = Maps.newHashMap();
		try {
			shopService.addShopUsers(id, userId, getUser());
			result.put(RESULT_TYPE, ResultType.SUCCESS.toInteger());
		} catch (Exception ex) {
			result.put(RESULT_TYPE,ResultType.ERROR.toInteger());
			DreamLogUtils.error(this, "shopUserAdd","add shop user occured exception", ex);
		}
		return result;
	}
	
}
